package com.viking.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RefreshScope 注解用于动态获取nacos配置中心值
 *
 */
@RefreshScope
@RestController
@RequestMapping("/config")
public class ConfigController {
    /**
     * 启动编译时，@value未配置相应值会进行扫描报错, 换使用 Environment
     */
//    @Value("${user.config.name}")
//    private String userConfigName;
//    @Value("${user.config.age}")
//    private String userConfigAge;

    @Autowired
    private Environment env;

    @RequestMapping("/get")
    public String test(){
        String userConfigName = env.getProperty("user.config.name");
        String userConfigAge = env.getProperty("user.config.age");
        String userConfigPhone = env.getProperty("user.config.phone");
        String result="userConfigName:"+userConfigName+",userConfigAge:"+userConfigAge+",userConfigPhone:"+userConfigPhone;
        System.out.println(result);
        return result;
    }
}
